package edu.hawaii.myisern.action;

import java.io.IOException;
import javax.xml.bind.JAXBException;

import edu.hawaii.myisern.model.Id;
import edu.hawaii.myisern.model.IsernManager;
import edu.hawaii.myisern.organizations.jaxb.Organization;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.util.Log;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

/**
 * Handles request from create or update an organization.
 * 
 * @author Kevin English
 */
public class OrganizationFormActionBean extends IsernActionBean {
  /** A private copy of the log **/
  private static Log log = Log.getInstance(OrganizationFormActionBean.class);
  /** An error string, always displayed, but invisible if empty. */
  private String errorMessage = "";
  /** Singleton for use by action bean only. */
  @ValidateNestedProperties( { @Validate(field = "name", required = true),
      @Validate(field = "type", required = true) })
  private Organization organization;
  private String id;

  /**
   * Get the organization.
   * 
   * @return A organization
   */

  public Organization getOrganization() {
    return this.organization;
    // this.organization.getResearchDescription();
  }

  /**
   * Sets the organization to edit or create.
   * 
   * @param organization The organization object.
   */
  public void setOrganization(Organization organization) {
    this.organization = organization;
  }

  /**
   * Set the Id string used to identify a record.
   * 
   * @param id The id, usually the name.
   */
  public void setId(String id) {
    this.id = id;
  }

  /**
   * Get the Id string used to identify a record.
   * 
   * @return The Id string.
   */
  public String getId() {
    return this.id;
  }

  /**
   * Returns the error message.
   * 
   * @return The error message.
   */
  public String getErrorMessage() {
    return this.errorMessage;
  }

  /**
   * For new collaboration, forward the user onto the jsp form.
   * 
   * @return The next jsp page.
   */
  @DefaultHandler
  public Resolution newOrganization() {
    return new ForwardResolution("organization_form.jsp");
  }

  /**
   * Loads a organization on to the form ready for editing.
   * 
   * @return The next jsp page.
   */
  @DontValidate
  public Resolution preEdit() {
    log.debug("Logging DEBUG");
    IsernManager im = new IsernManager();
    this.organization = im.getOrganization(new Id(this.id));
    // this.id = Id.getId(this.organization);
    return new ForwardResolution("organization_form.jsp");
    // return new RedirectResolution("/organization_form.jsp").flash(this);
  }

  /**
   * Saves (or updates) the organization.
   * 
   * @return The next jsp page.
   */
  public Resolution save() {

    IsernManager im = new IsernManager();

    if (isUpdate(im)) {
      log.debug("save is update");
      im.updateOrganization(new Id(this.id), this.organization);
      try {
        im.saveOrganizations();
      }
      catch (IOException e) {
        this.errorMessage = "Error: cannot save(),IO Exception";
        e.printStackTrace();
      }
      catch (JAXBException e) {
        this.errorMessage = "Error: cannot save(),JAXB Exception";
        e.printStackTrace();
      }
    }
    else {
      log.debug("save is create");
      im.createOrganization(this.organization);
    }

    return new RedirectResolution("/organizations.jsp");
  }

  /**
   * Tells program if the record is new or update.
   * 
   * @param im An IsernManager object.
   * @return true if the id exist and it is an update.
   */
  private boolean isUpdate(IsernManager im) {
    return (im.getOrganization(new Id(this.id)) != null);
  }

}